package cloud.maque.admin.controller;

import cloud.maque.admin.annotations.MaquerManager;
import cloud.maque.admin.annotations.TenantId;
import cloud.maque.admin.annotations.TenantQuery;
import cloud.maque.admin.domain.entity.MqUser;
import cloud.maque.admin.domain.vo.MqCreateUserModel;
import cloud.maque.admin.domain.vo.UserUpdatePwd;
import cloud.maque.admin.domain.vo.UserVo;
import cloud.maque.admin.service.MqUserService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cloud.maque.framework.common.response.ResponseBodyVo;
import cloud.maque.framework.security.ssoclient.utils.SsoUserInfoUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 4color
 * @description 用户信息
 * @date 2023-02-13
 */
@RestController
@RequestMapping(value = "/user")
@Api(tags = "运维-用户接口")
public class UserController {

    @Resource
    private MqUserService mqUserService;

    @Autowired
    SsoUserInfoUtils infoUtils;

    /**
     * 新增
     *
     * @author 4color
     * @date 2023/02/13
     **/
    @PostMapping("/save")
    @ApiOperation(value = "用户插入")
    @TenantQuery
    public ResponseBodyVo insert(@RequestBody MqCreateUserModel mqUser, @TenantId String tenantId) {
        mqUser.getUser().setTenantId(tenantId);
        return ResponseBodyVo.Success(mqUserService.saveUser(mqUser));
    }

    @PostMapping("/saveBySuperManager")
    @ApiOperation(value = "超级管理员添加租户下面的用户")
    @MaquerManager
    public ResponseBodyVo saveBySuperManager(@RequestBody MqCreateUserModel mqUser) {
        return ResponseBodyVo.Success(mqUserService.saveBySuperManager(mqUser));
    }

    /**
     * 刪除
     *
     * @author 4color
     * @date 2023/02/13
     **/
    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "用户删除")
    @TenantQuery
    public ResponseBodyVo<Boolean> delete(@PathVariable("id") String id, @TenantId String tenantId) {
        return ResponseBodyVo.Success(mqUserService.delete(id, tenantId));
    }


    /**
     * 查询 根据主键 id 查询
     *
     * @author 4color
     * @date 2023/02/13
     **/
    @GetMapping("/detail")
    @ApiOperation(value = "查询 根据主键 id 查询")
    public ResponseBodyVo<MqCreateUserModel> getUserInfoById(@RequestParam("id") @ApiParam("用户ID") String id) {
        return ResponseBodyVo.Success(mqUserService.getUserAllInfo(id));
    }

    /**
     * 更新密码
     *
     * @author 4color
     * @date 2023/02/13
     **/
    @PostMapping("/updatepwd")
    @ApiOperation(value = "更新密码")
    public ResponseBodyVo<Boolean> UpdatePwd(@RequestBody UserUpdatePwd userUpdatePwd) {
        return ResponseBodyVo.Success(mqUserService.UpdatePwd(userUpdatePwd));
    }


    /**
     * 更新用户禁用状态
     *
     * @author 4color
     * @date 2023/02/13
     **/
    @PostMapping("/changeState/{userId}")
    @ApiOperation(value = "更新用户禁用状态")
    public ResponseBodyVo<Boolean> ChangeState(@PathVariable("userId") String userId) {
        return ResponseBodyVo.Success(mqUserService.ChangeState(userId));
    }

    /**
     * 根据Token获取用户信息
     *
     * @author 4color
     * @date 2023/02/13
     **/
    @PostMapping("/userInfo")
    @ApiOperation(value = "根据Token获取用户信息")
    public ResponseBodyVo<MqUser> UserInfo() {
        String userId = infoUtils.getUserId();
        MqUser user = mqUserService.getUser(userId);
//        user.setTel(PhoneUtil.hideBetween(user.getTel()).toString());
//        user.setIdnumber("******");

        return ResponseBodyVo.Success(user);
    }

    /**
     * 查询 分页查询
     *
     * @author 4color
     * @date 2023/02/13
     **/
    @PostMapping("/list")
    @ApiOperation(value = "查询用户")
    @TenantQuery
    public ResponseBodyVo<PageInfo<MqUser>> pageList(@RequestBody PaginateDataResquest<UserVo> queryRequest) {
        PageInfo<MqUser> result = mqUserService.pageList(queryRequest);
        return ResponseBodyVo.Success(result);
    }

    /**
     * 获取用户的所有角色，树
     *
     * @param id
     * @return
     */
    @GetMapping("/roletree")
    @ApiOperation(value = "获取用户的所有角色树")
    public ResponseBodyVo<List<List<String>>> getUserRoleTree(@RequestParam("id") @ApiParam("用户ID") String id) {
        return ResponseBodyVo.Success(mqUserService.getUserRoleTree(id));
    }

}
